package cn.b.sky.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class SkyFileUtils {
  protected static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

  private static final Logger log = LoggerFactory.getLogger(SkyFileUtils.class);
  
  private SkyFileUtils() {
    // Hide the constructor
  }

  /**
   * 
   * <strong>Title : buildFilePath<br>
   * </strong> <strong>Description : </strong>文件保存路径<br>
   * <strong>Create on : 2015-10-6 下午6:16:02<br>
   * </strong>
   * <p>
   * 
   * @param bussiness
   *            业务代码
   * @return String
   * @throws
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static String buildFilePath(String bussiness) {
    String path = "";

    if (!StringUtils.isBlank(bussiness)) {
      path = path + bussiness + "/";
    }

    Date date = new Date();
    path = path + DateUtils.getDateUtils(DateUtils.YYYY_MM_DD).format(date) + "/";
    return path;
  }

  /**
   * 
   * <strong>Title : renameFileName<br>
   * </strong> <strong>Description : </strong>聪明吗文件名称<br>
   * <strong>Create on : 2015-10-8 上午10:16:03<br>
   * </strong>
   * <p>
   * 
   * @param oldName
   * @param newName
   *            void
   * @throws
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static String renameFileName(String oldName, String newName) {
    if (oldName.indexOf(".") >= 0) {
      String extName = oldName.substring(oldName.lastIndexOf(".")).toLowerCase();
      return newName + extName;
    }
    return null;
  }

  public static String getMD5String(byte[] bytes) {
    MessageDigest messagedigest = null;
    try {
      messagedigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      log.error("{}", e);
    }
    messagedigest.update(bytes);
    return bufferToHex(messagedigest.digest());
  }

  private static String bufferToHex(byte[] bytes) {
    return bufferToHex(bytes, 0, bytes.length);
  }

  private static String bufferToHex(byte[] bytes, int m, int n) {
    StringBuilder sb = new StringBuilder(2 * n);
    int k = m + n;
    for (int l = m; l < k; l++) {
      appendHexPair(bytes[l], sb);
    }
    return sb.toString();
  }

  private static void appendHexPair(byte bt, StringBuilder sb) {
    char c0 = hexDigits[(bt & 0xf0) >> 4];
    char c1 = hexDigits[bt & 0xf];
    sb.append(c0);
    sb.append(c1);
  }

}
